package br.com.sigad.persistencia.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;

public class DAOGenerico<T> extends DAOUtil implements IDAO<T> {

	@SuppressWarnings("rawtypes")
	private final Class entitiyClass;

	@SuppressWarnings("rawtypes")
	public DAOGenerico(Class entitiy) {
		this.entitiyClass = entitiy;

	}

	public T salvar(T entity) {
		final Session session = getSession();
		session.save(entity);
		session.flush();
		return entity;
	}

	public T remover(T entity) {
		final Session session = getSession();
		session.delete(entity);
		session.flush();
		return entity;
	}

	@SuppressWarnings("unchecked")
	public List<T> carregarTodos() {
		final Session session = getSession();
		final Criteria criteria = session.createCriteria(entitiyClass);
		return criteria.list();
	}

	@SuppressWarnings("rawtypes")
	public Class getObjectClass() {
		return this.entitiyClass;
	}

}
